package com.hxqh.bo.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hxqh.bo.bean.FpsPolicyInfo;
import com.hxqh.bo.dao.FpsPolicyInfoDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * FpsPolicyInfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.hxqh.bo.bean.FpsPolicyInfo
 * @author MyEclipse Persistence Tools
 */

public class FpsPolicyInfoDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(FpsPolicyInfoDAO.class);
	// property constants
	public static final String POLICY_NAME = "policyName";
	public static final String POLICY_INFO = "policyInfo";
	public static final String POLICY_PATH = "policyPath";
	public static final String POLICY_FILENAME = "policyFilename";
	public static final String POLICY_SUPPLYER = "policySupplyer";
	public static final String ISUSED = "isused";
	public static final String DELETED = "deleted";
	public static final String MAX_HOLD_NUM = "maxHoldNum";
	public static final String FUTURES_CODE = "futuresCode";
	public static final String FUTURES_TYPE = "futuresType";
	public static final String INTERNAL_TIME = "internalTime";
	public static final String SMALL_TYPE = "smallType";

	protected void initDao() {
		// do nothing
	}
	
	public List getFuturesCode() {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql = "select distinct futures_code from fps_policy_info where DELETED = 0 and  futures_code is not null";
				SQLQuery q = arg0.createSQLQuery(sql);
				q.addScalar("futures_code", Hibernate.STRING);
				return q.list();
			}
			
		});
	}
	
	public List getPolicyIds() {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				
				String sql = "select POLICY_ID from fps_policy_info where DELETED = 0 and POLICY_ID != 0";
				SQLQuery q = arg0.createSQLQuery(sql);
				q.addScalar("POLICY_ID", Hibernate.INTEGER);
				
				return q.list();
			}
			
		});
	}

	public void save(FpsPolicyInfo transientInstance) {
		log.debug("saving FpsPolicyInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(FpsPolicyInfo persistentInstance) {
		log.debug("deleting FpsPolicyInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public FpsPolicyInfo findById(java.lang.Integer id) {
		log.debug("getting FpsPolicyInfo instance with id: " + id);
		try {
			FpsPolicyInfo instance = (FpsPolicyInfo) getHibernateTemplate()
					.get("com.hxqh.bo.bean.FpsPolicyInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(FpsPolicyInfo instance) {
		log.debug("finding FpsPolicyInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding FpsPolicyInfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from FpsPolicyInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPolicyName(Object policyName) {
		return findByProperty(POLICY_NAME, policyName);
	}

	public List findByPolicyInfo(Object policyInfo) {
		return findByProperty(POLICY_INFO, policyInfo);
	}

	public List findByPolicyPath(Object policyPath) {
		return findByProperty(POLICY_PATH, policyPath);
	}

	public List findByPolicyFilename(Object policyFilename) {
		return findByProperty(POLICY_FILENAME, policyFilename);
	}

	public List findByPolicySupplyer(Object policySupplyer) {
		return findByProperty(POLICY_SUPPLYER, policySupplyer);
	}

	public List findByIsused(Object isused) {
		return findByProperty(ISUSED, isused);
	}

	public List findByDeleted(Object deleted) {
		return findByProperty(DELETED, deleted);
	}

	public List findByMaxHoldNum(Object maxHoldNum) {
		return findByProperty(MAX_HOLD_NUM, maxHoldNum);
	}

	public List findByFuturesCode(Object futuresCode) {
		return findByProperty(FUTURES_CODE, futuresCode);
	}

	public List findByFuturesType(Object futuresType) {
		return findByProperty(FUTURES_TYPE, futuresType);
	}

	public List findByInternalTime(Object internalTime) {
		return findByProperty(INTERNAL_TIME, internalTime);
	}

	public List findBySmallType(Object smallType) {
		return findByProperty(SMALL_TYPE, smallType);
	}

	public List findAll() {
		log.debug("finding all FpsPolicyInfo instances");
		try {
			String queryString = "from FpsPolicyInfo where deleted = 0 and policyId != 0";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findAllPolicy() {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				
				String sql = "select a.POLICY_ID,a.POLICY_NAME,a.FUTURES_CODE," +
						"a.FUTURES_TYPE,b.ALL_TRADE_NUM,b.MAX_HIGH_LOST," +
						"a.MAX_HOLD_NUM,b.PROFIT_LOST,b.RETURN_RATE " +
						"from fps_policy_info a, fps_policy_statistics_info b " +
						"where a.deleted = 0 and a.POLICY_ID = b.POLICY_ID;";
				System.out.println(sql);
				SQLQuery q = arg0.createSQLQuery(sql);
				
				q.addScalar("POLICY_ID", Hibernate.INTEGER);
				q.addScalar("POLICY_NAME", Hibernate.STRING);
				q.addScalar("FUTURES_CODE", Hibernate.STRING);
				q.addScalar("FUTURES_TYPE", Hibernate.STRING);
				q.addScalar("ALL_TRADE_NUM", Hibernate.INTEGER);
				q.addScalar("MAX_HIGH_LOST", Hibernate.DOUBLE);
				q.addScalar("MAX_HOLD_NUM", Hibernate.INTEGER);
				q.addScalar("PROFIT_LOST", Hibernate.DOUBLE);
				q.addScalar("RETURN_RATE", Hibernate.DOUBLE);
				return q.list();
			}
			
		});
	}

	public FpsPolicyInfo merge(FpsPolicyInfo detachedInstance) {
		log.debug("merging FpsPolicyInfo instance");
		try {
			FpsPolicyInfo result = (FpsPolicyInfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(FpsPolicyInfo instance) {
		log.debug("attaching dirty FpsPolicyInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(FpsPolicyInfo instance) {
		log.debug("attaching clean FpsPolicyInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static FpsPolicyInfoDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (FpsPolicyInfoDAO) ctx.getBean("FpsPolicyInfoDAO");
	}
}